<?php

$t = new randomSets('noAnalyse/log.imslex.lex.noAnalyse.txt','imslex.lex.noAna.txt');
$t = new randomSets('noAnalyse/log.wiki.lex.noAnalyse.txt','wiki.lex.noAna.txt');
$t = new randomSets('noAnalyse/log.nzz.lex.noAnalyse.txt','nzz.lex.noAna.txt');

class randomSets {
	
	function __construct($set,$filename){
		$this->set = $this->getItems($set);
		$this->selection = array();
		
		// init random
		mt_srand(time());
		$n = count($this->set);
		echo $n."\n";
		$anz = 0;
		$limit = ($n<100) ? $n : 100;
		while($anz < $limit){
			// get next random word
		    $next = (rand() % $n);
			// if selection is ok,
		    if(isset($this->set[$next])){
			    $this->selection[] = $this->set[$next];
			    unset($this->set[$next]);
				$anz++;
		    }
			else
			    continue;
		}
		
		$this->writeSet($filename);
		
	}
	
	function writeSet($name){
		echo "Writing, ".count($this->selection)." items.\n";
		$handle = fopen("randomSets/".$name.".txt","w");
		foreach($this->selection as $item)
			fwrite($handle,$item[0]."\n".implode("\n",$item[1])."\n\n");
		fclose($handle);
	}
	
	function getItems($file){
		$arr = array();
		
		echo $file."\n";
		
		if(!($handle = fopen("logs/".$file,"r"))) die("Konnte Datei nicht öffnen!\n");
		$key = "";
		$items = array();
		while(!feof($handle)) {
			$line = trim(fgets($handle));
			if(!preg_match('/[\^\+]+/',$line)){ // schließt Item
				if(!empty($key)){
					$arr[] = array($key,$items);
				}
				$key = trim($line);
				$items = array();
			} else {
				$items[] = trim($line);
			}
			
		}
		return $arr;	
	}
	
}


?>